﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _8280WEBSocket
{
    public partial class PersonForm : frmNodeForm
    {
        public PersonForm(INMain main) : base(main)
        {
            InitializeComponent();
        }
        /// <summary>
        /// 读取所有人员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetAllPerson_Click(object sender, EventArgs e)
        {
            var cd = mMainForm.GetCommandDetail();
            var cmd = new Command.Person.GetPersonList(cd, 0);
            cd.CommandCompleteEvent += (s, c) =>
            {
                var result = c.Result as Command.Person.GetPersonListResult;
                mMainForm.AddLog("读取所有人员信息成功:" + Environment.NewLine + ConvertJsonString(result.JsonValue));
            };
            mMainForm.AddCommand(cmd);
        }
        /// <summary>
        /// 清空所有人员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClearAllPerson_Click(object sender, EventArgs e)
        {
            var cd = mMainForm.GetCommandDetail();
            var cmd = new Command.Person.DeletePerson(cd, 0, 1);
            cd.CommandCompleteEvent += (s, c) =>
            {
                var result = c.Result as Command.CommandResult;
                mMainForm.AddLog("清空人员信息成功:" + Environment.NewLine + ConvertJsonString(result.JsonValue));
            };
            mMainForm.AddCommand(cmd);
        }
        /// <summary>
        /// 根据用户号获取人员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetPerson_Click(object sender, EventArgs e)
        {
            var cd = mMainForm.GetCommandDetail();
            var cmd = new Command.Person.GetPerson(cd, 0);
            cd.CommandCompleteEvent += (s, c) =>
            {
                var result = c.Result as Command.Person.GetPersonResult;
                mMainForm.AddLog("获取人员信息成功:" + Environment.NewLine + ConvertJsonString(result.JsonValue));
            };
            mMainForm.AddCommand(cmd);
        }
        /// <summary>
        /// 根据用户号删除人员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeletePerson_Click(object sender, EventArgs e)
        {
            var cd = mMainForm.GetCommandDetail();
            var cmd = new Command.Person.DeletePerson(cd, 0);
            cd.CommandCompleteEvent += (s, c) =>
            {
                var result = c.Result as Command.CommandResult;
                mMainForm.AddLog("删除人员信息成功:" + Environment.NewLine + ConvertJsonString(result.JsonValue));
            };
            mMainForm.AddCommand(cmd);
        }
        /// <summary>
        /// 机器注册人员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRequireEnroll_Click(object sender, EventArgs e)
        {
            var cd = mMainForm.GetCommandDetail();
            cd.Timeout = 10000;
            var person = new Command.Person.RequestRequireEnroll_DTO
            {
                code = uint.Parse(txtECode.Text),
                name = txtEName.Text,
                Group = txtEGroup.Text,
                Job = txtEJob.Text,
                EnrollType = (byte)(cmbEnrollType.SelectedIndex + 1),
            };
            var cmd = new Command.Person.RequireEnrollPerson(cd, person);
            cd.CommandCompleteEvent += (s, c) =>
            {
                var result = c.Result as Command.Person.RequireEnrollPersonResult;
                mMainForm.AddLog("机器注册人员成功:" + Environment.NewLine + ConvertJsonString(result.JsonValue));
            };
            mMainForm.AddCommand(cmd);
        }
        private void btnAddPerson_Click(object sender, EventArgs e)
        {
            var cd = mMainForm.GetCommandDetail();
            var person = new Command.Person.Person_DTO
            {
                Name = txtName.Text,
                Code = uint.Parse(txtCode.Text),
                Card = ulong.Parse(txtCard.Text),
                Pin = txtPin.Text,
                Group = txtGroup.Text,
                Job = txtJob.Text,
                Tp = (byte)cmbTp.SelectedIndex,
                Times = short.Parse(txtTimes.Text),
                Start = dtpStart.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                End = dtpEnd.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                Admin = (byte)cmbAdmin.SelectedIndex,
                Status = (byte)cmbStatus.SelectedIndex,
                Keep = (byte)cmbKeep.SelectedIndex,
                FeatureData = null,
                UseHol = (byte)cmbUseHol.SelectedIndex,
                Attachment = null,
                Relays = txtRelays.Text
            };

            cd.CommandCompleteEvent += (s, c) =>
            {
                var result = c.Result as Command.CommandResult;
                mMainForm.AddLog("添加人员成功:" + Environment.NewLine + ConvertJsonString(result.JsonValue));
            };
            var cmd = new Command.Person.AddPerson(cd, person);
            mMainForm.AddCommand(cmd);

        }
    }
}
